import numpy as np
import csv
import random
from datetime import datetime
from time import strftime

'''
Global and Local Empirical Bayes Smoothers with Gamma Model
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def readRandomResult(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    row_title = ra.fieldnames
    #print row_title

    for item in row_title[1:]:
        tSplitU = item.split('_')
        tId = int(tSplitU[0][1:])
        tSplitP = tSplitU[1].split('(')
        tLLR = float(tSplitP[0][:-1])
        tShape = float(tSplitP[1][:-1])
        sKey.append([tId, tLLR, tShape])

    sKey = np.array(sKey)
    sKey.shape=(-1, 3)
    output = []
    for item in np.unique(sKey[:,0]):
        output.append([int(item)])
    #print output

    for item in sKey:
        output[int(item[0])-1].append(item[1])
        output[int(item[0])-1].append(item[2])

    highestLLR = []
    highestMeasure = []
    for item in output:
        tLLRList = []
        tMeasureList = []
        for i in range((len(item)-1)/2):
            tLLRList.append(item[2*i+1])
            tMeasureList.append(item[2*i+2]*item[2*i+1])
        highestLLR.append(max(tLLRList))
        highestMeasure.append(max(tMeasureList))
    return sKey, output, highestLLR, highestMeasure

def readResult(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    row_title = ra.fieldnames
    #print row_title

    for item in row_title[1:]:
        tSplitU = item.split('_')
        tId = int(tSplitU[0][1:])
        tSplitP = tSplitU[1].split('(')
        tLLR = float(tSplitP[0][:-1])
        tShape = float(tSplitP[1][:-1])
        sKey.append([tId, tLLR, tShape])

    sKey = np.array(sKey)
    sKey.shape=(-1, 3)
    output = []
    for item in np.unique(sKey[:,0]):
        output.append([int(item)])
    #print output

    for item in sKey:
        output[int(item[0])].append(item[1])
        output[int(item[0])].append(item[2])

    highestLLR = []
    highestMeasure = []
    for item in output:
        tLLRList = []
        tMeasureList = []
        for i in range((len(item)-1)/2):
            tLLRList.append(item[2*i+1])
            tMeasureList.append(item[2*i+2]*item[2*i+1])
        highestLLR.append(max(tLLRList))
        highestMeasure.append(max(tMeasureList))
        
    return sKey, output, highestLLR, highestMeasure

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    #unitCSV = 'C:/_DATA/CancerData/SatScan/mult6000/three16_modify/three16_format_modify.csv'
    #filepath = 'C:/_DATA/CancerData/SatScan/mult6000/three16_modify/iRedistrict/KL/'
    filepath = 'C:/_DATA/CancerData/SatScan/mult6000/three16_modify/iRedistrict/KL_TABU_8/'
    #resultCSV = filepath + 'redistricting__rd_1.0_total.csv'
    resultCSV = filepath + 'redistricting__rd_1.0_random_total.csv'

    #dataMatrix = build_data_list(unitCSV)
    output, outputGroup, highestLLR, highestMeasure = readRandomResult(resultCSV)
    #output, outputGroup, highestLLR, highestMeasure = readResult(resultCSV)
    #print highestLLR
    #print output
    #writer = csv.writer(open(resultCSV[:-4] + '_group.csv', "wb"))
    #writer.writerows(outputGroup)
    np.savetxt(resultCSV[:-4] + '_formate.csv', output, delimiter=',', fmt = '%10.5f')
    np.savetxt(resultCSV[:-4] + '_selectedLLR.csv', highestLLR, delimiter=',', fmt = '%10.5f')
    np.savetxt(resultCSV[:-4] + '_selectedMeasure.csv', highestMeasure, delimiter=',', fmt = '%10.5f')

    print "end at " + getCurTime()
    print "==========================="

           
